home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1996 October / MACPOWER-1996-10.ISO.7z / MACPOWER-1996-10.ISO / AMUG / Internet_31 / NetCacheResolver 0.9d6 / NetCacheResolver 0.9d6 ト / library / NCR_Event.c < prev    next >
Text File  |  1996-05-12  |  8KB  |  402 lines

  1. // NetCache Resolver, 1995 (C) Mizutori Tetsuya
  2. // - NCR_Event.c, October 8, 1995
  3. // This document is pretty printed in 10-point Geneva font.
  4.  
  5. #include "NCR_Basic.h"
  6. #include "NCR_Event.h"
  7. #ifdef DEBUG
  8. #include "NCR_Message.h"
  9. #endif /* DEBUG */
  10.  
  11. // definition
  12.  
  13. // global
  14. Boolean    gDone        = false;
  15. Boolean    gAEOpenAppl    = false;
  16. Boolean    gAEOpenDocs    = false;
  17. Boolean    gAEPrintDoc    = false;
  18. Boolean    gAEQuitAppl    = false;
  19.  
  20. // callback function, defined in 'main.c'
  21. extern OSErr DoOpen ( WindowPtr window );    
  22.  
  23.  
  24. // prototype
  25. /***** DoEvent *****/
  26. static void DoEvent ( EventRecord *event );
  27. static OSErr MyInspectAppleEvent ( const EventRecord *event );
  28. /***** Do suspent & resume *****/
  29. static void DoSuspendResume ( EventRecord *event );
  30. /***** Do Activate & Update*****/
  31. static void DoActivate ( WindowPtr window, Boolean becomingActive );
  32. static void DoUpdate ( WindowPtr window, EventRecord *event );
  33. /***** HandleMouseDown *****/
  34. static void HandleMouseDown ( EventRecord *eventPtr );
  35. static void DoContent ( WindowPtr window, EventRecord *event );
  36. static void DoKeyDown ( short c );
  37. /***** Handle MenuChoice *****/
  38. static void HandleMenuChoice ( long menuChoice );
  39. static void HandleAppleChoice ( short item );
  40. static void HandleFileChoice ( short item );
  41. static void HandleEditChoice ( short theItem );
  42.  
  43.  
  44. /***** setup *****/
  45. void ToolboxInit ( void )
  46. {
  47.     long    i;
  48.     for ( i=0; i<5; i++ ) MoreMasters();    // allocates a block of 64 Handles
  49.  
  50.     InitGraf( &qd.thePort );
  51.     InitFonts();
  52.     InitWindows();
  53.     InitMenus();
  54.     FlushEvents( everyEvent, 0 );
  55.     TEInit();
  56.     InitDialogs( (long) nil );
  57.     InitCursor();
  58.     AEObjectInit();            // for AppleScript Resolver
  59.     ZeroScrap();
  60.     MaxApplZone();
  61. }
  62.  
  63.  
  64. //#define    kMenuBarID    128
  65. //#define    mApple        128
  66. //#define    mFile        mApple+1
  67. //#define    mEdit        mApple+2
  68. //#define    mPosition        100
  69.  
  70. #define    kNotANormalMenu        -1
  71.  
  72. void SetupMenuBar ( void )
  73. {
  74.     Handle        menuBar;
  75.     MenuHandle    menu;
  76.     OSErr        err;
  77.  
  78.     menuBar = GetNewMBar( kMenuBarID );
  79.     if ( menuBar == nil ) return;
  80.  
  81.     SetMenuBar( menuBar );
  82.  
  83.     menu = GetMenuHandle( mApple );
  84.     AppendResMenu( menu, 'DRVR' );
  85.  
  86. //    menu = GetMenu( mFont );
  87. //    AppendResMenu( menu, 'FONT' );
  88. //    InsertMenu( menu, kNotANormalMenu );
  89.     
  90.     menu = GetMenu( mPosition );
  91.     InsertMenu( menu, kNotANormalMenu );
  92.  
  93.     DrawMenuBar();
  94. }
  95.  
  96. /***** EventLoop *****/
  97. #define    kSleep    10000000L
  98.  
  99. void EventLoop ( void )
  100. {
  101.     EventRecord    event;
  102.     
  103. #ifdef DEBUG
  104.     Message("Starting EventLoop ...¥n");
  105. #endif /* DEBUG */
  106.  
  107.     gDone = false;
  108.     gAEOpenAppl = false;
  109.     gAEOpenDocs = false;
  110.     gAEQuitAppl = false;
  111.  
  112.     while ( true ) {
  113.         if ( !gAEOpenAppl && gAEOpenDocs ) break;
  114.         if ( gAEOpenAppl && gDone ) break;
  115.         if ( gAEQuitAppl ) break;
  116.  
  117.         if ( WaitNextEvent( everyEvent, &event, kSleep, nil ) )
  118.             DoEvent( &event );
  119.         
  120.         SystemTask();
  121.     }
  122. }
  123.  
  124. /***** DoEvent *****/
  125. static void DoEvent ( EventRecord *event )
  126. {
  127.     short        theChar;
  128.     Boolean    toActive;
  129.  
  130.     switch ( event->what ) {
  131.         case kHighLevelEvent:
  132.             if ( event->message == kCoreEventClass ) 
  133.                 switch ( *((long *) (&(event->where))) ) {
  134.                     case kAEOpenApplication:    gAEOpenAppl = true; break;
  135.                     case kAEOpenDocuments:    gAEOpenDocs = true; break;
  136.                     case kAEPrintDocuments:    gAEPrintDoc = true; break;
  137.                     case kAEQuitApplication:    gAEQuitAppl = true; break;
  138.                     default:                break;
  139.                 }
  140.             MyInspectAppleEvent( event );
  141.             AEProcessAppleEvent( event );
  142.             break;
  143.         case mouseDown:
  144.             HandleMouseDown( event );
  145.             break;
  146.         case mouseUp:
  147.         case keyUp:
  148.             break;    
  149.         case keyDown:
  150.         case autoKey:
  151.             theChar = event->message & charCodeMask;
  152.             if ( (event->modifiers & cmdKey) != 0 ) {
  153.                 //AdjustMenu();
  154.                 HandleMenuChoice( MenuKey( theChar ) ); }
  155.             else
  156.                 DoKeyDown( (unsigned char) theChar );
  157.             break;
  158.         case updateEvt:
  159.             DoUpdate( (WindowPtr) (event->message), event );
  160.             break;
  161.         case activateEvt:
  162.             toActive = ( (event->modifiers & activeFlag) == activeFlag );
  163.             DoActivate( (WindowPtr) (event->message), toActive );
  164.             break;
  165.         case diskEvt:
  166.         case networkEvt:
  167.         case driverEvt:
  168.         case app1Evt:
  169.         case app2Evt:
  170.         case app3Evt:
  171.             break;
  172.         case osEvt:
  173.             DoSuspendResume( event );
  174.     }
  175. }
  176.  
  177. /***** Inspect Event Record *****/
  178. static OSErr MyInspectAppleEvent ( const EventRecord *event )
  179. {
  180.     OSType    eventClass, classID;
  181.     
  182.     if ( event->what != kHighLevelEvent ) return noErr;
  183.     
  184.     eventClass = (OSType) event->message;
  185.     classID =  * (OSType *) &(event->where);
  186.  
  187. #ifdef DEBUG
  188.     Message("===: eventClass('%T') classID('%T')¥n", eventClass, classID );
  189. #endif /* DEBUG */
  190.  
  191.     return noErr;
  192. }
  193.  
  194.  
  195. /***** Do suspent & resume *****/
  196. static void DoSuspendResume ( EventRecord *event )
  197. {
  198.     WindowPtr        window;
  199.     long            message;
  200.  
  201.     window = FrontWindow();
  202.     message = event->message;
  203.  
  204.      switch ( ( message >> 24 ) & 0xFF ) {
  205.         case mouseMovedMessage:
  206.             break;
  207.         case suspendResumeMessage:    
  208.             switch ( ( message & 0x03 ) ) {
  209.                 case 1:        /* Resume */
  210.                     DoActivate( window, true );
  211.                     break;
  212.                 case 2:        /* Suspend */
  213.                     DoActivate( window, false );
  214.                     break;
  215.                 case 3:        /* Clipboard changed */
  216.                     break;
  217.             }
  218.             break;
  219.     }
  220. }
  221.  
  222. /***** Do Activate *****/
  223. static void DoActivate ( WindowPtr window, Boolean becomingActive )
  224. {    
  225.     GrafPtr    oldPort;
  226.  
  227.     GetPort( &oldPort );
  228.     SetPort( window );
  229.  
  230.     if ( becomingActive ) {
  231.         /* do activate, here */
  232.     } else {
  233.         /* do deactivate, here */
  234.     }
  235.  
  236.     SetPort( oldPort );
  237. }
  238.  
  239.  
  240. /***** Do Update *****/
  241. static void DoUpdate ( WindowPtr window, EventRecord *event )
  242. {
  243.     GrafPtr    oldPort;
  244.  
  245.     GetPort( &oldPort );
  246.     SetPort( window );
  247.  
  248.     BeginUpdate( window );
  249.     /* redraw window, here */
  250.     EndUpdate( window );
  251.     
  252.     SetPort( oldPort );
  253. }
  254.  
  255.  
  256. /***** HandleMouseDown *****/
  257. static void HandleMouseDown ( EventRecord *event )
  258. {
  259.     WindowPtr        window;
  260.     long            menuChoice;
  261.     long            thePart;
  262.     
  263.     thePart = FindWindow( event->where, &window );
  264.     
  265.     switch( thePart ) {
  266.         case inMenuBar:
  267.             //AdjustMenu();
  268.             menuChoice = MenuSelect( event->where );
  269.             HandleMenuChoice( menuChoice );
  270.             break;
  271.         case inSysWindow:
  272.             SystemClick( event, window );
  273.             break;
  274.         case inContent:
  275.             if ( window != FrontWindow() ) {
  276.                 SelectWindow( window );
  277.             } else {
  278.                 DoContent( window, event );
  279.             }
  280.             break;
  281.         case inDrag:
  282.             DragWindow( window, event->where, &qd.screenBits.bounds );
  283.             break;
  284.         case inGoAway:
  285.             if ( TrackGoAway( window, event->where ) )
  286.                 gDone = true;
  287.             break;
  288.         case inZoomIn:
  289.         case inZoomOut:
  290.             if ( TrackBox( window, event->where, thePart ) ) {
  291.                 /* do nothing, here */
  292.             }
  293.             break;
  294.     }
  295. }
  296.  
  297. static void DoContent ( WindowPtr window, EventRecord *event )
  298. {
  299. }
  300.  
  301. static void DoKeyDown ( short c )
  302. {
  303. }
  304.  
  305. /***** Handle MenuChoice *****/
  306. static void HandleMenuChoice ( long menuChoice )
  307. {
  308.     short        menu;
  309.     short        item;
  310.  
  311.     if ( menuChoice != 0 ) {
  312.         menu = HiWord( menuChoice );
  313.         item = LoWord( menuChoice );
  314.         switch ( menu ) {
  315.             case mApple:
  316.                 HandleAppleChoice( item );
  317.                 break;
  318.             case mFile:
  319.                 HandleFileChoice( item );
  320.                 break;
  321.             case mEdit:
  322.                 HandleEditChoice( item );
  323.                 break;
  324.             default:
  325.                 break; }
  326.  
  327.         HiliteMenu( 0 );
  328.     }
  329. }
  330.  
  331. /***** Handle Apple Choice *****/
  332. #define    kAboutID        130
  333.  
  334. static void HandleAppleChoice ( short item )
  335. {
  336.     MenuHandle    appleMenu;
  337.     Str31        accName;
  338.     short            accNumber;
  339.  
  340.     switch ( item ) {
  341.         case iAbout:
  342.             Alert( kAboutID, nil );
  343.             break;
  344.         default:
  345.             appleMenu = GetMHandle( mApple );
  346.             GetItem( appleMenu, item, accName );
  347.             accNumber = OpenDeskAcc( accName );
  348.             break;
  349.     }
  350. }
  351.  
  352. /***** Handle File Choice *****/
  353. static void HandleFileChoice ( short item )
  354. {
  355.     WindowPtr        window;
  356.     
  357.     window = FrontWindow();
  358.     
  359.     switch ( item ) {
  360.         case iQuit:
  361.             gDone = true;
  362.             break;
  363.         case iOpen:
  364.             DoOpen( window );
  365.             break;
  366.         case iNew:
  367.         case iClose:
  368.         case iSave:
  369.         case iSaveAs:
  370.         case iPageSetup:
  371.         case iPrint:
  372.         case iPreferences:
  373.         default:
  374.             break;
  375.     }
  376. }
  377.  
  378. /***** Handle Edit Choice *****/
  379. static void HandleEditChoice ( short theItem )
  380. {
  381.     WindowPtr        window;
  382.     short            item;
  383.  
  384.     if ( SystemEdit( theItem - 1 ) ) return;
  385.  
  386.     window = FrontWindow();
  387.     
  388.     switch ( theItem ) {
  389.         case iUndo:
  390.         case iCut:
  391.         case iCopy:
  392.         case iPaste:
  393.         case iClear:
  394.         case iDuplicate:
  395.         case iSelectAll:
  396.         default:
  397.             break;
  398.     }
  399. }
  400.  
  401. // end of program
  402.